//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <dbtables.hpp>
#include "Main.h"
#include "Customers.h"
#include "CustomerDlg.h"
#include "CustomerReport.h"
#include "FormUtilities.h"
#include "PurchaseDlg.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
__fastcall TfrmCustomers::TfrmCustomers(TComponent* Owner, TDatabase * dbMain)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   dbsMain = dbMain;
   bridge = new TCustomerBridge( dbsMain);
}

void TfrmCustomers::SetButtonsState()
{
   btnUpdate->Enabled = !tblCustomers->IsEmpty();
   btnDelete->Enabled = btnUpdate->Enabled;
   btnReport->Enabled  = btnUpdate->Enabled;

   if ( tblCustomers->IsEmpty())
      lblName->Caption = "";
}

//---------------------------------------------------------------------------
void __fastcall TfrmCustomers::btnFindClick(TObject *Sender)
{
   tblCustomers->IndexFieldNames = "NAME";
   tblCustomers->FindNearest( ARRAYOFCONST((edtName->Text)));
   tblCustomers->Refresh();
}
//---------------------------------------------------------------------------



void __fastcall TfrmCustomers::btnUpdateClick(TObject *Sender)
{
   TfrmCustDlg * frmCustDlg;

   frmCustDlg = new TfrmCustDlg( this,
                                 bridge,
                                 tblCustomers->FieldByName( "ID")->AsInteger,
                                 tblCustomers->FieldByName( "CUSTOMER_ID")->AsString,
                                 tblCustomers->FieldByName( "NAME")->AsString,
                                 tblCustomers->FieldByName( "LAST_NAME")->AsString,
                                 tblCustomers->FieldByName( "PHONE")->AsString,
                                 tblCustomers->FieldByName( "ADDRESS")->AsString,
                                 tblCustomers->FieldByName( "EMAIL")->AsString);
   try
   {
      if ( frmCustDlg->ShowModal() == mrOk)
      {
         tblCustomers->Refresh();
         tblCheckings->Refresh();
         SetButtonsState();
      }
   }
   __finally
   {
      delete frmCustDlg;
   }   

}
//---------------------------------------------------------------------------

void __fastcall TfrmCustomers::btnAddClick(TObject *Sender)
{
   TfrmCustDlg * frmCustDlg;

   frmCustDlg = new TfrmCustDlg( this, bridge);

   try
   {
      if ( frmCustDlg->ShowModal() == mrOk)
      {
         tblCustomers->Refresh();
         tblCheckings->Refresh();
         SetButtonsState();
      }
   }
   __finally
   {
      delete frmCustDlg;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmCustomers::btnDeleteClick(TObject *Sender)
{
   if ( MessageDlg( "Are you sure ?",
                    mtWarning,
                    TMsgDlgButtons() << mbYes << mbNo,
                    0) == mrYes)
   {
      bridge->Delete( tblCustomers->FieldByName( "ID")->AsInteger);
      tblCustomers->Refresh();
      tblCheckings->Refresh();
      SetButtonsState();
   }
}
//---------------------------------------------------------------------------


void __fastcall TfrmCustomers::FormDestroy(TObject *Sender)
{
   delete bridge;
}
//---------------------------------------------------------------------------

void __fastcall TfrmCustomers::srcCustomersDataChange(TObject *Sender,
      TField *Field)
{
   if ( !tblCustomers->IsEmpty())
   {
      tblCheckings->Close();
      tblCheckings->Filter = "ID = " + tblCustomers->FieldByName( "ID")->AsString;
      tblCheckings->Open();
      tblCheckings->Refresh();
      lblName->Caption = tblCustomers->FieldByName( "NAME")->AsString + " " + tblCustomers->FieldByName( "LAST_NAME")->AsString;
   }   
}
//---------------------------------------------------------------------------

void __fastcall TfrmCustomers::btnCloseClick(TObject *Sender)
{
   Close();        
}
//---------------------------------------------------------------------------



void __fastcall TfrmCustomers::FormShow(TObject *Sender)
{
   SetButtonsState();        
}
//---------------------------------------------------------------------------

void __fastcall TfrmCustomers::btnClearClick(TObject *Sender)
{
   edtName->Clear();        
}
//---------------------------------------------------------------------------

void __fastcall TfrmCustomers::btnReportClick(TObject *Sender)
{
   TfrmCustomerReport * frmCustomerReport;

   frmCustomerReport = new TfrmCustomerReport( this, tblCustomers->FieldByName( "NAME")->AsString + " " + tblCustomers->FieldByName( "LAST_NAME")->AsString, tblCheckings);

   try
   {
      frmCustomerReport->Preview();
   }
   __finally
   {
      delete frmCustomerReport;
   }
}
//---------------------------------------------------------------------------


void __fastcall TfrmCustomers::grdCustomersKeyDown(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
   switch ( Key)
   {
      case VK_INSERT :
         btnAddClick( Sender);
         break;

      case VK_DELETE :
         if ( btnDelete->Enabled)
            btnDeleteClick( Sender);
         break;

      case VK_SPACE :
         if ( btnUpdate->Enabled)
            btnUpdateClick( Sender);
         break;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmCustomers::grdCheckingsDrawColumnCell(TObject *Sender,
      const TRect &Rect, int DataCol, TColumn *Column,
      TGridDrawState State)
{
   if (( Column->FieldName == "DATE") && (( double)Column->Field->AsDateTime == 0))
        grdCheckings->Canvas->FillRect( Rect);
}
//---------------------------------------------------------------------------



void __fastcall TfrmCustomers::btnPurchasesClick(TObject *Sender)
{
   TfrmPurchaseDlg* frmPurchaseDlg = new TfrmPurchaseDlg(this, dbsMain);

   try
   {
      frmPurchaseDlg->ShowModal();
   }
   __finally
   {
      delete frmPurchaseDlg;   
   }
}
//---------------------------------------------------------------------------



void __fastcall TfrmCustomers::btnFindByLastNameClick(TObject *Sender)
{
   tblCustomers->IndexFieldNames = "LAST_NAME";
   tblCustomers->FindNearest( ARRAYOFCONST((edtName->Text)));
   tblCustomers->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TfrmCustomers::btnFindByIDClick(TObject *Sender)
{
   tblCustomers->IndexFieldNames = "CUSTOMER_ID";
   tblCustomers->FindNearest( ARRAYOFCONST((edtName->Text)));
   tblCustomers->Refresh();
}
//---------------------------------------------------------------------------

